<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      class="form-search"
    >
      <el-form-item label="工序名称" prop="workProcedureName">
        <el-input
          v-model="queryParams.workProcedureName"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 190px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['technology:procedure:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      row-key="id"
      border
      :data="procedureList"
      show-overflow-tooltip
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="工序名称" class-name="leftClassCell" prop="workProcedureName" />
      <el-table-column label="工序编码" prop="workProcedureCode">
        <template #default="scope">
          <el-link type="primary" @click="handleDetail(scope.row)">{{
            scope.row.workProcedureCode
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="工序描述" prop="remark" />
      <el-table-column
        label="操作"
        class-name="small-padding fixed-width"
        width="280"
      >
        <template #default="scope">
          <el-button
            v-if="!scope.row.parentNo"
            link
            type="primary"
            icon="Plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['technology:procedure:add']"
            >新增二级工序</el-button
          >
          <el-button
            link
            icon="edit"
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['technology:procedure:edit']"
            >编辑</el-button
          >
          <el-button
            link
            icon="delete"
            type="primary"
            @click="handleDelete(scope.row)"
            v-hasPermi="['technology:procedure:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工序管理对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" v-model="open" width="700px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :disabled="isRead"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item v-if="!isFirstLevel" label="上级工序" prop="parentNo">
          <el-select
            v-model="form.parentNo"
            placeholder="请输入上级工序"
            disabled
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="item in firstLevel"
              :key="item.id"
              :label="item.workProcedureName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="工序编码" prop="workProcedureCode">
          <code-input
            v-model="form.workProcedureCode"
            code="TECH_WORK_PROCEDURE_CODE"
            :disabled="!!form.id"
            placeholder="请输入工序编码"
          />
        </el-form-item>
        <el-form-item label="工序名称" prop="workProcedureName">
          <el-input
            v-model="form.workProcedureName"
            placeholder="请输入工序名称"
          />
        </el-form-item>

        <div v-if="!isFirstLevel">
          <el-form-item label="单位生产时长" prop="unitProdDuration">
            <el-input
              v-model="form.unitProdDuration"
              placeholder="请输入单位生产时长"
            >
              <template #append>秒</template>
            </el-input>
          </el-form-item>
        </div>
        <el-form-item label="工序描述" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入工序描述"
            :maxlength="200"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button v-if="!isRead" type="primary" @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProcedure,
  getProcedure,
  updateProcedure,
  addProcedure,
  delProcedure,
} from "@/api/technology/procedure";
import { listType } from "@/api/base/equipment/equipmentTypeApi";
import DetailData from "./data";
export default {
  name: "Procedure",
  data() {
    return {
      listType,
      // 遮罩层
      loading: true,
      isRead: false,
      refreshTable: true,
      isExpandAll: true,
      // 选中数组
      ids: [],
      // 一级工序列表
      firstLevel: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序管理表格数据
      procedureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isFirstLevel: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workProcedureName: null,
        deleteFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workProcedureCode: [
          { required: true, message: "工序编码不能为空", trigger: "blur" },
        ],
        workProcedureName: [
          { required: true, message: "工序名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.nextTick(() => {
        this.refreshTable = true;
      });
    },
    load(row, treeNode, resolve) {
      getChildren(row.id).then((res) => {
        resolve(res.rows);
      });
    },
    /** 查询工序管理列表 */
    getList() {
      this.loading = true;
      listProcedure(this.queryParams).then((response) => {
        // handleTree(data, id, parentId, children)
        this.procedureList = response.rows;
        this.firstLevel = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workProcedureCode: null,
        workProcedureName: null,
        workProcedureDesc: null,
        parentNo: null,
        deviceTypeNo: null,
        deviceTypeName: null,
        stationNo: null,
        stationName: null,
        unitProdDuration: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.isRead = false;
      this.isFirstLevel = !row.id;
      this.reset();
      if (row.id) {
        this.form.parentNo = row.id;
        this.title = "新增二级工序";
      } else {
        this.title = "新增工序";
      }
      this.open = true;
    },
    handleDetail(row) {
      this.isFirstLevel = !row.parentNo;
      if (this.isFirstLevel) {
        this.isRead = true;
        getProcedure(row.id).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "工序详情";
        });
      } else {
        this.subPage(DetailData, { title:this.isFirstLevel?'工序详情':"二级工序详情", data: row }, () => {
          this.getList();
        });
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isRead = false;
      this.isFirstLevel = !row.parentNo;
      const id = row.id || this.ids;
      getProcedure(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = this.isFirstLevel?'编辑工序':"编辑二级工序";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProcedure(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcedure(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if (row.children && row.children.length) {
        this.$modal.msgWarning("该工序下存在子工序，请先删除子工序");
        return;
      }
      const ids = row.id || this.ids;
      
      this.$modal
        .confirm(`是否确认删除工序名称为“${row.workProcedureName || ''}”的工序？`)
        .then(function () {
          return delProcedure(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "technology/procedure/export",
        {
          ...this.queryParams,
        },
        `procedure_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
